package com.qunshang.common;

import com.mycompany.yourtrade.model.UserBean;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import nl.captcha.Captcha;

/**
 *
 * @author xiaoguang
 */
public class SessionUtils {
    
     static String SESSION_KEY = "UserBeanSession";
     /**
	 * 往session中存放登录信息
	 * 
	 * @param request
	 * @param loginInfo
	 */
//	public  static void setAttribute(HttpServletRequest request,UserBean userBean){
//		
//		HttpSession session = request.getSession(true);
//                
//		session.setAttribute(SESSION_KEY, userBean.getId());
//	}
	
	/**
	 * 从session中获取信息
	 * 
	 * @param request
	 * @param key
	 * @return
	 */
//	public static Object getAttribute(HttpServletRequest request,String key){
//		
//            Object result = null;
//		HttpSession session = request.getSession(false);
//		if (session.getAttribute(SESSION_KEY) != null) {
//                    result = session.getAttribute(SESSION_KEY);
//                }
//		return result;
//	}
	
	
	/**
	 * 往session中存放信息
	 * 
	 * @param request
	 * @param key
	 * @param value
	 */
//    public static void setAttribute(HttpServletRequest request,String key ,Object value){
//        
//        HttpSession session = request.getSession(true);
//        session.setAttribute(key, value);
//    }
     public static String getUserId(final HttpServletRequest request) {
        UserBeanSession session = getSession(request);
        if (session == null) {
            return null;
        }

        UserBean user = session.getUser();
        return user == null ? null : user.getId();
    }

    public static UserBean getUser(final HttpServletRequest request) {
        UserBeanSession session = getSession(request);
        if (session == null) {
            return null;
        }
        return session.getUser();
    }

    public static void setSession(final HttpServletRequest request,
                                  final UserBeanSession session) {
        request.getSession().setAttribute(SESSION_KEY, session);
    }

    public static UserBeanSession getSession(final HttpServletRequest request) {
        if (request == null) {
            return null;
        }
        HttpSession httpSession = request.getSession(false);
        return httpSession == null ? null : getSession(httpSession);
    }

    public static UserBeanSession getSession(final HttpSession httpSession) {
        if (httpSession == null) {
            return null;
        }
        return (UserBeanSession) httpSession.getAttribute(SESSION_KEY);
    }

    /**
     * 获取保存在Session中的对象
     *
     * @param <T>
     * @param request
     * @param name 在Session中的键值
     * @param clazz 对象类型
     * @param remove 是否从Session中移除
     * @return 返回对象 or null
     */
    public static <T> T getAttribute(final HttpServletRequest request,
                                     final String name,
                                     final Class<T> clazz,
                                     final boolean remove) {
        T result = null;
        HttpSession session = request.getSession();
        if (session.getAttribute(name) != null) {
            result = (T) session.getAttribute(name);
            if (remove) {
                session.removeAttribute(name);
            }
        }
        return result;
    }
    /**
     * 判断验证码是否正确
     *
     * @param request
     * @param captcha
     * @return
     */
    public static boolean validateCaptcha(final HttpServletRequest request,
                                          final String captcha) {
        Captcha c = (Captcha) SessionUtils.getAttribute(request, Captcha.NAME, Captcha.class, false);
        return c.isCorrect(captcha);
    }
}
